//
//  KLLocalVideoCell.m
//  WebRTCDemo_iOS
//
//  Created by szblsx2 on 2020/3/9.
//  Copyright © 2020 szblsx2. All rights reserved.
//

#import "KLLocalVideoCell.h"
#import <Masonry/Masonry.h>

@interface KLLocalVideoCell ()
//渲染视图
@property (nonatomic, strong) RTCCameraPreviewView *previewView;

@property (nonatomic, strong) UIImageView *adminIcon;
//@property (nonatomic, strong) KLHeadView *headIcon;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation KLLocalVideoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.previewView];
        self.clipsToBounds = YES;
        [self addSubview:self.adminIcon];
        [self.adminIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(self);
            make.width.equalTo(@(40));
            make.height.equalTo(@(20));
        }];
        
//        [self addSubview:self.headIcon];
//        [self.headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self);
//            make.height.width.equalTo(@(60));
//        }];
        
        [self addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.adminIcon);
            make.left.equalTo(@(45));
        }];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.previewView.frame = self.bounds;
}


- (void)reloadCellWithLocalUser:(KLMeetUser *)localUser focus:(BOOL)focus
{
    if (focus && _openLocalVideo) {
        if (localUser.localCaptureSession) {
             _previewView.captureSession = localUser.localCaptureSession;
        }
    }else{
        _previewView.captureSession = nil;
    }
    
    self.adminIcon.hidden = !localUser.isManager;
    self.nameLabel.text = localUser.userName;
    
    if (localUser.isManager) {
        [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(45));
        }];
    }else{
        [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(5));
        }];
    }
}

#pragma mark - Getter

- (RTCCameraPreviewView *)previewView{
    if (!_previewView) {
        _previewView = [[RTCCameraPreviewView alloc] initWithFrame:self.bounds];
        AVCaptureVideoPreviewLayer *videoLayer = (AVCaptureVideoPreviewLayer *)_previewView.layer;
        videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    }
    return _previewView;
}

- (UIImageView *)adminIcon
{
    if (!_adminIcon) {
        _adminIcon = [[UIImageView alloc] init];
        _adminIcon.image = [UIImage imageNamed:@"adminIcon"];
        _adminIcon.hidden = YES;
    }
    return _adminIcon;
}

//- (KLHeadView *)headIcon
//{
//    if (!_headIcon) {
//        _headIcon = [[KLHeadView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
//    }
//    return _headIcon;;
//}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:13];
    }
    return _nameLabel;
}

@end
